package com.guigu.www.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.pojo.Authorization;
import com.guigu.www.pojo.PageVo;
import com.guigu.www.service.AuthorizationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class AuthorizationController {
    @Autowired
    AuthorizationService service;

    @RequestMapping("queryauthorization.action")
    public PageVo queryauthorization(Authorization authorization,
                                     @RequestParam(value = "pagenum",defaultValue = "1") int pagenum,
                                     @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){
        QueryWrapper queryWrapper=new QueryWrapper();
        if(StringUtils.isNotBlank(authorization.getAuname())){
            queryWrapper.like("auname",authorization.getAuname());
        }
        Page page=service.page(new Page<Authorization>(pagenum,pagesize),queryWrapper);
        PageVo<Authorization> pageVo=new PageVo<>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }
    @RequestMapping("addauthorization.action")
    public Map addauthorization(Authorization authorization){
        Map map=new HashMap();
        boolean res=service.save(authorization);
        if(res){
            map.put("msg","添加成功");
        }else {
            map.put("msg","添加失败");
        }
        return map;
    }
    @RequestMapping("updateauthorization.action")
    public Map updateauthorization(Authorization authorization){
        Map map=new HashMap();
        boolean res=service.updateById(authorization);
        if(res){
            map.put("msg","修改成功");
        }else {
            map.put("msg","修改失败");
        }
        return map;
    }
    @RequestMapping("querybyauid.action/{auid}")
    public Authorization querybyauid(@PathVariable int auid){
        Authorization res=service.getById(auid);
        return res;
    }
    @RequestMapping("deleteauthorization.action/{auid}")
    public Map deleteauthorization(@PathVariable int auid){
        Map map=new HashMap();
        boolean res=service.removeById(auid);
        if(res){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
        }
        return map;
    }
}
